<?php 
	require_once 'core/initialize.php';	
	require_once "includes/header.php";

	$user = new User();

	if ($user->isLoggedIn()) {

		Redirect::to('dashboard.php');

		if ($user->hasPermission('admin')) {
			echo "Administrator";
		}

	}else {
	}

	if (Input::exists()) {
		if (Token::check(Input::get('token'))) {

			//echo "this has been run </br>";
	
			$validate = new Validation();
			$validation = $validate->check($_POST, array(
			
				'username' => array(
					'required' => true ),
				'password' => array(
					'required' => true )
			
			));

			if ($validation->passed()) {
				$user = new User();
				$remember = (Input::get('remember') === 'on') ? true : false;

				$login = $user->login(Input::get('username'), Input::get('password'), $remember);

				if ($login) {
					Redirect::to('dashboard.php');
					
				}else {
					echo "<div class='input_error'> Incorrect Username or Password </div>";
				}
			}else {
				
				foreach ($validation->errors() as $error) {
					echo "<div class='input_error'>" . $error . "</div>";
				}
				
			}

		}
	}

?>
<div class="section group">
	<div class="column div_1_of_2 componentDesignA">
		<div class="column div_1_of_1 widgetHeaderTheme">
			<h1>Lorem ipsum dolor</h1>
		</div>
		<p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form</p>
		<hr/>	
		<div class="column div_1_of_1">
			<a href="register.php"  class="widgetButtonTheme">Register</a>
		</div>
	</div>	
	<div class="column div_1_of_2 componentDesignA">
		<form action="" method="post">
			<div class="section clearing">
				<div class="column div_1_of_1 widgetHeaderTheme">
					<h1>Login</h1>
				</div>
			</div>
			<div class="section clearing">
				<div class="column div_1_of_1 widgetTransBgTheme">
					<input type="text" name="username" placeholder="Username"/>
				</div>
			</div>
			<div class="section clearing">	
				<div class="column div_1_of_1 widgetTransBgTheme">
					<input type="password" name="password" placeholder="Password"/>
				</div>
			</div>
			<div class="section clearing">
				<div class="column div_1_of_1">
					<input type="hidden" name="token" value="<?php  echo Token::generate();?>"/>
					<input type="submit" name="submit" value="Login"/>
				</div>
			</div>
		</form><!-- END OF LOGIN FORM -->
	</div>
</div>

<?php require_once "includes/footer.php"; ?>